using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Game.Objects;
using Game.Types;

namespace SL
{
    public class VehicleListboxItem : Grid
    {
        readonly VehicleType _VehicleType;
        Rectangle _Rectangle;

        public VehicleListboxItem(
            VehicleType vehicleType,
            GameBuilding gameBuilding)
        {
            _VehicleType = vehicleType;

            InitStackPanel();
            InitRectangle();

            if (gameBuilding.Tank.IsGreaterThan(vehicleType.Cost))
            {
                _Rectangle.Opacity = 0;
                IsEnabled = true;
            }
            else
            {
                _Rectangle.Opacity = 0.7;
                IsEnabled = false;
            }
        }

        public VehicleType VehicleType
        {
            get { return _VehicleType; }
        }

        void InitStackPanel()
        {
            var panel = new StackPanel {Orientation = Orientation.Horizontal};
            Children.Add(panel);
            {
                var image = new Image
                {
                    Source = _VehicleType.Picture,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
                panel.Children.Add(image);
            }
            {
                AddText(panel, "Material" + "\r\n" + _VehicleType.Cost.Material.Value);
                AddText(panel, "Fuel" + "\r\n" + _VehicleType.Cost.Fuel.Value);
                AddText(panel, "Energy" + "\r\n" + _VehicleType.Cost.Energy.Value);
            }
            {
                var text = new TextBlock
                {
                    Text = _VehicleType.Name,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Center,
                    TextAlignment = TextAlignment.Center,
                    Padding = new Thickness(8)
                };
                panel.Children.Add(text);
            }
        }

        private void AddText(
            StackPanel panel,
            string title)
        {
            var text = new TextBlock
            {
                Text = title,
                Width = 70,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center
            };
            var border = new Border
            {
                Width = 70,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Stretch,
                BorderBrush = new SolidColorBrush(Colors.DarkGray),
                BorderThickness = new Thickness(1),
                Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x9E, 0xE5, 0x9E)),
                Padding = new Thickness(3),
                Child = text
            };
            panel.Children.Add(border);
        }

        void InitRectangle()
        {
            _Rectangle = new Rectangle
            {
                Width=1000,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                Fill = new SolidColorBrush(Colors.Gray)
            };
            Children.Add(_Rectangle);
        }

    }
}